package com.qf.agro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.CustomException;
import com.qf.agro.common.ResultInfo;
import com.qf.agro.mapper.FacilitiesCategoryMapper;
import com.qf.agro.mapper.FacilitiesMapper;
import com.qf.agro.pojo.Facilities;
import com.qf.agro.pojo.FacilitiesCategory;
import com.qf.agro.service.FacilitiesCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.service.impl
 * @Date 2023/7/3 17:56
 */
@Service
public class FacilitiesCategoryServiceImpl implements FacilitiesCategoryService {
    @Autowired
    private FacilitiesCategoryMapper facilitiesCategoryMapper;

    @Autowired
    private FacilitiesMapper facilitiesMapper;
    @Override
    public ResultInfo getPage(Long page, Long pageSize, String name) {
        Page<FacilitiesCategory> pages=new Page<>(page,pageSize);
        LambdaQueryWrapper<FacilitiesCategory> lqw=new LambdaQueryWrapper<>();
        pages= facilitiesCategoryMapper.selectPage(pages, lqw);

        return ResultInfo.success(pages);
    }

    @Override
    public ResultInfo save(FacilitiesCategory facilitiesCategory) {
        int insert = facilitiesCategoryMapper.insert(facilitiesCategory);
        return insert>0?ResultInfo.success("添加成功"):ResultInfo.error("添加失败");
    }

    @Override
    public ResultInfo deleteById(Long id) {
        LambdaQueryWrapper<Facilities> lqw=new LambdaQueryWrapper();
        lqw.eq(Facilities::getCategoryId,id);
        Integer count = facilitiesMapper.selectCount(lqw);

        if(count>0){
            throw new CustomException("有关联的设施，不能删除，请先删除关联字段");
        }
        facilitiesMapper.deleteById(id);
        return ResultInfo.success("删除成功");
    }

    @Override
    public ResultInfo update(FacilitiesCategory facilitiesCategory) {
      boolean flag=  facilitiesCategoryMapper.updateByMapper(facilitiesCategory);
        return flag?ResultInfo.success("添加成功"):ResultInfo.error("添加失败");
    }
}
